package com.example.seabedmusic.views;


import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.SettingsAdapter;
import com.example.seabedmusic.entity.SettingItem;
import com.example.seabedmusic.utils.UserManager;
import java.util.ArrayList;
import java.util.List;

public class AccountSecurityActivity extends AppCompatActivity implements SettingsAdapter.OnSettingItemClickListener {

    private RecyclerView rvAccountSecurity;
    private SettingsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_list);

        initViews();
        setupAccountSecurityList();
    }

    private void initViews() {
        rvAccountSecurity = findViewById(R.id.rvSettings);
        rvAccountSecurity.setLayoutManager(new LinearLayoutManager(this));

        // 设置标题
        setTitle("账号与安全");
    }

    private void setupAccountSecurityList() {
        List<SettingItem> items = new ArrayList<>();

        items.add(new SettingItem("修改密码", R.drawable.ic_password, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("绑定手机", R.drawable.ic_phone, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("绑定邮箱", R.drawable.ic_email, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("第三方账号绑定", R.drawable.ic_link, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("登录设备管理", R.drawable.ic_devices, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("账号注销", R.drawable.ic_delete, SettingItem.TYPE_LOGOUT));

        adapter = new SettingsAdapter(items);
        adapter.setOnSettingItemClickListener(this);
        rvAccountSecurity.setAdapter(adapter);
    }

    @Override
    public void onSettingItemClick(SettingItem item) {
        switch (item.getTitle()) {
            case "修改密码":
                showChangePasswordDialog();
                break;
            case "绑定手机":
                showBindPhoneDialog();
                break;
            case "绑定邮箱":
                showBindEmailDialog();
                break;
//            case "第三方账号绑定":
//                navigateToThirdPartyBinding();
//                break;
//            case "登录设备管理":
//                navigateToDeviceManagement();
//                break;
            case "账号注销":
                showAccountDeletionDialog();
                break;
        }
    }

    private void showChangePasswordDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("修改密码");

        // 创建输入布局
        android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
        layout.setOrientation(android.widget.LinearLayout.VERTICAL);
        layout.setPadding(50, 40, 50, 10);

        final android.widget.EditText etOldPassword = new android.widget.EditText(this);
        etOldPassword.setHint("当前密码");
        etOldPassword.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);

        final android.widget.EditText etNewPassword = new android.widget.EditText(this);
        etNewPassword.setHint("新密码");
        etNewPassword.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);

        final android.widget.EditText etConfirmPassword = new android.widget.EditText(this);
        etConfirmPassword.setHint("确认新密码");
        etConfirmPassword.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);

        layout.addView(etOldPassword);
        layout.addView(etNewPassword);
        layout.addView(etConfirmPassword);

        builder.setView(layout);

        builder.setPositiveButton("确定", (dialog, which) -> {
            String oldPassword = etOldPassword.getText().toString();
            String newPassword = etNewPassword.getText().toString();
            String confirmPassword = etConfirmPassword.getText().toString();

            if (newPassword.equals(confirmPassword) && newPassword.length() >= 6) {
                Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "密码不一致或长度不足6位", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showBindPhoneDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("绑定手机");

        android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
        layout.setOrientation(android.widget.LinearLayout.VERTICAL);
        layout.setPadding(50, 40, 50, 10);

        final android.widget.EditText etPhone = new android.widget.EditText(this);
        etPhone.setHint("请输入手机号");
        etPhone.setInputType(android.text.InputType.TYPE_CLASS_PHONE);

        final android.widget.EditText etCode = new android.widget.EditText(this);
        etCode.setHint("验证码");
        etCode.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);

        layout.addView(etPhone);
        layout.addView(etCode);

        builder.setView(layout);
        builder.setPositiveButton("绑定", (dialog, which) -> {
            Toast.makeText(this, "手机绑定成功", Toast.LENGTH_SHORT).show();
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showBindEmailDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("绑定邮箱");

        android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
        layout.setOrientation(android.widget.LinearLayout.VERTICAL);
        layout.setPadding(50, 40, 50, 10);

        final android.widget.EditText etEmail = new android.widget.EditText(this);
        etEmail.setHint("请输入邮箱地址");
        etEmail.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

        layout.addView(etEmail);

        builder.setView(layout);
        builder.setPositiveButton("绑定", (dialog, which) -> {
            String email = etEmail.getText().toString();
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                Toast.makeText(this, "邮箱绑定成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void navigateToThirdPartyBinding() {
//        Intent intent = new Intent(this, ThirdPartyBindingActivity.class);
//        startActivity(intent);
    }

    private void navigateToDeviceManagement() {
//        Intent intent = new Intent(this, DeviceManagementActivity.class);
//        startActivity(intent);
    }

    private void showAccountDeletionDialog() {
        new android.app.AlertDialog.Builder(this)
                .setTitle("账号注销")
                .setMessage("确定要注销账号吗？此操作不可逆，所有数据将被永久删除！")
                .setPositiveButton("确定注销", (dialog, which) -> {
                    // 执行注销逻辑
                    UserManager.getInstance(this).logout();
                    Toast.makeText(this, "账号已注销", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                })
                .setNegativeButton("取消", null)
                .show();
    }
}